<?php
namespace app\index\controller;

use app\common\controller\Base;
use app\index\model\Heroes;
use app\index\model\Item;
use app\ux\UtilXml;

/**
 * 英雄
 */
class Hero extends Base
{
    use \app\index\controller\traits\User;

    /**
     * 表字段初始化
     */
    public function _initialize()
    {

        //配置表信息

        $this->prop_path  = "./static/xml/acc_dragon_prop.xml";
        $this->hero_path  = "./static/xml/acc_dragon_hero.xml";
        $this->skill_name = "./static/xml/acc_dragon_skill.xml";
        $this->skill_path = "./static/xml/acc_hero_skill.xml";

        $this->prop = $this->getPropArr($this->prop_path);
        $this->hero = $this->getHeroArr($this->hero_path);

        //依赖顺序
        $this->skill_name = $this->getSkillNameArr($this->skill_name);
        $this->skill      = $this->getSkillArr($this->skill_path);

        parent::_initialize();
    }

    /**
     * 英雄信息查询
     */
    public function queryBasicInfo()
    {

        //服务器ID
        $sid = array_key_exists("sid", $_REQUEST) ? $_REQUEST['sid'] : null;

        //角色ID和昵称
        $id       = array_key_exists("id", $_REQUEST) ? $_REQUEST['id'] : null;
        $nickname = array_key_exists("nickname", $_REQUEST) ? $_REQUEST['nickname'] : null;

        //角色ID查询
        $id = $this->getUserId($id, $nickname);

        //查询反馈
        if (!$id) {
            _callback(array('success' => true, 'msg' => "提示:没有找到玩家信息", 'type' => "user-not-exist"));
        }

        $skill = $this->querySkillById($id);

        if ($skill) {

            //字段转换
            $r = $this->getPropArrByCid($id, $skill);
            $r = $this->getHeroNameById($r);

            $return['rows']    = $r ? $r : array();
            $return['total']   = count($r);
            $return['success'] = true;
        } else {
            $return['rows']    = array();
            $return['total']   = 0;
            $return['success'] = true;
        }

        _callback($return);
    }
    /**
     * 技能查询
     */
    public function querySkillById($id = null)
    {

        $temp = Heroes::where("`characterid`='{$id}'")
            ->field("`id`,`heroid`,`skill1`,`skill1_lv`,`skill2`,`skill2_lv`,`skill3`,`skill3_lv`,`skill4`,`skill4_lv`,`skill5`,`skill5_lv`,`skill6`,`skill6_lv`")
            ->order("`heroid` ASC")
            ->select();
        $rows = [];

        foreach ($temp as $data) {
            $item = $data->getData();
            array_push($rows, $item);
        }

        $skill = $this->skill;
        $res   = array();
        foreach ($rows as $k => $v) {
            $res[$k]['id']     = $v['id'];
            $res[$k]['heroid'] = $v['heroid'];
            $res[$k]['skill1'] = (0 == $v['skill1']) ? '未解锁' : $skill[$v['skill1'] - 1]["skill_name"][$v['skill1_lv'] - 1] . ' lv' . $v['skill1_lv'];
            $res[$k]['skill2'] = (0 == $v['skill2']) ? '未解锁' : $skill[$v['skill2'] - 1]["skill_name"][$v['skill2_lv'] - 1] . ' lv' . $v['skill2_lv'];
            $res[$k]['skill3'] = (0 == $v['skill3']) ? '未解锁' : $skill[$v['skill3'] - 1]["skill_name"][$v['skill3_lv'] - 1] . ' lv' . $v['skill3_lv'];
            $res[$k]['skill4'] = (0 == $v['skill4']) ? '未解锁' : $skill[$v['skill4'] - 1]["skill_name"][$v['skill4_lv'] - 1] . ' lv' . $v['skill4_lv'];
            $res[$k]['skill5'] = (0 == $v['skill5']) ? '未解锁' : $skill[$v['skill5'] - 1]["skill_name"][$v['skill5_lv'] - 1] . ' lv' . $v['skill5_lv'];
            $res[$k]['skill6'] = (0 == $v['skill6']) ? '未解锁' : $skill[$v['skill6'] - 1]["skill_name"][$v['skill6_lv'] - 1] . ' lv' . $v['skill6_lv'];
        }
        return $res;
    }
    /**
     * 装备查询
     */
    public function queryItemsById($id = null, $heroid)
    {

        $temp = Item::where("`characterid`='{$id}' AND `heroid`='{$heroid}'")
            ->field("`cid`,`heroid`")
            ->limit(4)
            ->select();
        $rows = [];

        foreach ($temp as $data) {
            $item = $data->getData();
            array_push($rows, $item);
        }
        return ($rows);
    }
    /**
     * 配置表获取
     */
    public function getPropArr($xml_file)
    {

        $xmlNode = simplexml_load_file($xml_file);
        $temp    = UtilXml::xmlToArray($xmlNode);
        $temp    = $temp ? $temp["RECORDS"]["RECORD"] : $temp;
        if ($temp) {
            $rtn = array();
            foreach ($temp as $k => $v) {
                $rtn[$v['id']]['name']     = $v['name'];
                $rtn[$v['id']]['position'] = $v['position'];
            }
        }
        return $rtn;
    }
    /**
     * 配置表获取
     */
    public function getHeroArr($xml_file)
    {

        $xmlNode = simplexml_load_file($xml_file);
        $temp    = UtilXml::xmlToArray($xmlNode);
        $temp    = $temp ? $temp["RECORDS"]["RECORD"] : $temp;
        if ($temp) {
            $rtn = array();
            foreach ($temp as $k => $v) {
                $rtn[$v['id']]['name'] = $v['name'];
            }
        }
        return $rtn;
    }
    /**
     * 配置表获取
     */
    public function getSkillNameArr($xml_file)
    {

        $xmlNode = simplexml_load_file($xml_file);
        $temp    = UtilXml::xmlToArray($xmlNode);
        $temp    = $temp ? $temp["RECORDS"]["RECORD"] : $temp;
        if ($temp) {
            $rtn = array();
            foreach ($temp as $k => $v) {
                $rtn[$v['id']]['name']        = $v['name'];
                $rtn[$v['id']]['description'] = $v['description'];
            }
        }
        return $rtn;
    }
    /**
     * 配置表获取
     */
    public function getSkillArr($xml_file)
    {

        $skill_name = $this->skill_name;
        $xmlNode    = simplexml_load_file($xml_file);

        $temp = UtilXml::xmlToArray($xmlNode);
        $temp = $temp ? $temp["RECORDS"]["RECORD"] : $temp;

        if ($temp) {
            $rtn = array();
            foreach ($temp as $k => $v) {
                $arr      = explode(";", $v['skill_id']);
                $name_arr = array();
                foreach ($arr as $ko => $vo) {
                    $name_arr[$ko] = $skill_name[$vo]['name'];
                }
                //$rtn[$k]['skill_id']=$arr;
                $rtn[$k]['skill_name'] = $name_arr;
            }
        }
        return $rtn;
    }
    /**
     * 道具配置表查询
     */
    public function getPropArrByCid($id, $skill)
    {
        $arr = $this->prop;

        foreach ($skill as $k => $v) {
            $items = $this->queryItemsById($id, $v['heroid']);

            if ($items) {
                foreach ($items as $kk => $vv) {
                    if (array_key_exists($vv['cid'], $arr)) {
                        $prop = $arr[$vv['cid']];

                        switch ($prop["position"]) {
                            case "1":
                                $skill[$k]['weapon'] = $prop["name"];
                                break;
                            case "2":
                                $skill[$k]['costume'] = $prop["name"];
                                break;
                            case "3":
                                $skill[$k]['shoes'] = $prop["name"];
                                break;
                            case "4":
                                $skill[$k]['decorations'] = $prop["name"];
                                break;
                        }
                    }
                }
            } else {
                $skill[$k]['weapon']      = "";
                $skill[$k]['costume']     = "";
                $skill[$k]['shoes']       = "";
                $skill[$k]['decorations'] = "";
            }
        }
        return $skill;
    }
    /**
     * 英雄配置表查询
     */
    public function getHeroNameById($r = array())
    {
        $arr = $this->hero;

        foreach ($r as $k => $v) {
            if (array_key_exists($v['heroid'], $arr)) {
                $r[$k]['heroid'] = $arr[$v['heroid']]['name'];
            }
        }
        return $r;
    }
}
